'''
Created on 2011-10-19

@author: tony.li.fly@gmail.com
'''

from xyh.oa.models import MenuType
from django.http import HttpResponse
from django.utils import simplejson as json

def listMenus(request):
    
    login_user = request.session['user']
    has_menus = []
    for _role in login_user.roles.all():
        for _menu in _role.menus.all():
            if not has_menus.__contains__(_menu.id):
                has_menus.append(_menu.id)
    
    allTypes = MenuType.objects.all();
    allTypesJSON = []
    for _type in allTypes:
        _typeJSON = {'name':_type.name,'seq':_type.seq}
        _menus = []
        for _menu in _type.menu_set.all():
            if has_menus.__contains__(_menu.id):
                _menus.append({"name":_menu.name,"icon":_menu.icon,"operation_function":_menu.operation_function,"seq":_menu.seq})
        if len(_menus) != 0:
            _typeJSON["children"] = _menus
            allTypesJSON.append(_typeJSON)
    
    return HttpResponse(json.dumps(allTypesJSON))
